import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  { path: '/login', component: () => import('@/views/login') },
  {
    path: '/',
    component: () => import('@/views/layout'),
    redirect: '/article',
    children: [
      { path: '/article', component: () => import('@/views/article') },
      { path: '/dashboard', component: () => import('@/views/dashboard') }
    ]
  }
]
const whitePage = ['/login']
const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (store.state.token) {
    next()
  } else if (whitePage.includes(to.path.toLowerCase())) {
    next()
  } else {
    next('/login')
  }
})

export default router
